package com.niiuzu.client.screen.feed.filter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;

/**
 * Implémentation de la vue de la gestion des flux RSS.
 */
public class FeedFilterViewImpl extends Composite implements
		FeedFilterView {

	/**
	 * Interface {@link UiBinder} adaptée à la vue
	 * {@link FeedFilterViewImpl}.
	 */
	interface SuitedUiBinder extends UiBinder<Widget, FeedFilterViewImpl> {

	}

	@UiField
	FlexTable channelTable;
	@UiField
	Anchor addChannelAnchor;
	@UiField
	Anchor addChannelGroupAnchor;

	@Override
	public void createView() {
		SuitedUiBinder uiBinder = GWT.create(SuitedUiBinder.class);
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public String toString() {
		return this.getClass().toString();
	}

	/**
	 * Implémentation pour le résultat de l'ajout d'un flux dans la vue.
	 */
	private class AddChannelResultImpl implements AddChannelResult {
		private Integer rowIndex;
		private HasClickHandlers hasClickHandlers;

		/**
		 * Construit une nouvelle instance de {@link AddChannelResultImpl}.
		 */
		public AddChannelResultImpl(Integer rowIndex,
				HasClickHandlers hasClickHandlers) {
			this.rowIndex = rowIndex;
			this.hasClickHandlers = hasClickHandlers;
		}

		@Override
		public HasClickHandlers getHasClickHandlers() {
			return hasClickHandlers;
		}

		@Override
		public Integer getRowIndex() {
			return rowIndex;
		}

	}

	@Override
	public AddChannelResult addChannel(String channelName) {
		int i = 0;
		int row = channelTable.getRowCount();
		Button removeButton = new Button("X");
		channelTable.setWidget(row, i++, removeButton);
		channelTable.setText(row, i++, channelName);

		return new AddChannelResultImpl(row, removeButton);
	}

	@Override
	public void removeChannel(Integer rowIndex) {
		channelTable.clearCell(rowIndex.intValue(), 0);
		channelTable.clearCell(rowIndex.intValue(), 1);
	}

	@Override
	public void clearChannelList() {
		channelTable.removeAllRows();
	}

	@Override
	public HasClickHandlers getAddChannelHasClickHandlers() {
		return addChannelAnchor;
	}

	@Override
	public HasClickHandlers getAddChannelGroupHasClickHandlers() {
		return addChannelGroupAnchor;
	}
}
